<?php

defined('G_IN_SYSTEM')or exit('No permission resources.');
System::load_app_fun("pay","pay");
System::load_sys_fun("user");
System::load_app_class("tocode","pay",'no');
class pay {
	private $db;
	private $members;		//会员信息
	private $MoenyCount; 	//商品总金额
	private $shops; 		//商品信息
	private $pay_type;		//支付类型
	private $fukuan_type;	//付款类型 买商品 充值
	private $dingdan_query = true;	//订单的	mysql_qurey 结果
	public $pay_type_bank = false;

	public $scookie = null;
	public $fufen = 0;
	public $fufen_to_money = 0;

    public $query_1 = false;

	//初始化类数据
	//$addmoney 充值金额
	public function init($uid=null,$pay_type=null,$fukuan_type='',$addmoney=''){
		$this->db=System::load_sys_class('model');
		$this->db->Autocommit_start();
		$this->members = $this->db->GetOne("SELECT * FROM `@#_member` where `uid` = '$uid' for update");

		if($this->pay_type_bank){
			$pay_class = $this->pay_type_bank;
			$this->pay_type =$this->db->GetOne("SELECT * from `@#_pay` where `pay_class` = '$pay_class' and `pay_start` = '1'");
			$this->pay_type['pay_bank'] = $pay_type;

		}
		if(is_numeric($pay_type)){
			$this->pay_type =$this->db->GetOne("SELECT * from `@#_pay` where `pay_id` = '$pay_type' and `pay_start` = '1'");
			$this->pay_type['pay_bank'] = 'DEFAULT';
		}

		$this->fukuan_type=$fukuan_type;
		if($fukuan_type=='go_record'){
			return $this->go_record($uid);
		}
		if($fukuan_type=='addmoney_record'){
			return $this->addmoney_record($addmoney);
		}
		return false;
	}

	//买商品
	private function go_record($userId){


//		if(is_array($this->scookie)){
//			$Cartlist = $this->scookie;
//		}else{
//			$Cartlist=json_decode(stripslashes(_getcookie('Cartlist')),true);
//		}
        $sql = "SELECT cart FROM `@#_cart` where `uid` = $userId";
	$this->writeLogFile("record_sql:");
	$this->writeLogFile($sql);
	$values = $this->db->GetList($sql);
	$this->writeLogFile("values==");
	$this->writeLogFile($values);
	$this->writeLogFile("values==end");
        foreach ($values as $key => $val) {
            $data = json_decode($val['cart'], true);
            $this->Cartlist = $data;
        }
        $Cartlist = $this->Cartlist;

        $shopids='';			//商品ID
        if(is_array($Cartlist)){
            foreach($Cartlist as $key => $val){
                $shopids.=intval($key).',';
            }
            $shopids=str_replace(',0','',$shopids);
            $shopids=trim($shopids,',');

        }

        $shoplist=array();		//商品信息
        if($shopids!=NULL){
            $shoplist=$this->db->GetList("SELECT * FROM `@#_shoplist` where `id` in($shopids) and `q_uid` is null for update",array("key"=>"id"));
        }else{
            $this->db->Autocommit_rollback();
            return '购物车内没有商品!';
        }

        $MoenyCount= 0;
        $shopguoqi = 0;
        if(count($shoplist)>=1){
            $scookies_arr = array();
            $scookies_arr['MoenyCount'] = 0;
            foreach($Cartlist as $key => $val){
                $key=intval($key);
                if(isset($shoplist[$key]) && $shoplist[$key]['shenyurenshu'] != 0){
                    if(($shoplist[$key]['xsjx_time'] != '0') && $shoplist[$key]['xsjx_time'] < time()){
                        unset($shoplist[$key]);
                        $shopguoqi = 1;
                        continue;
                    }
                    $shoplist[$key]['cart_gorenci']=$val['num'] ? $val['num'] : 1;
                    if($shoplist[$key]['cart_gorenci'] >= $shoplist[$key]['shenyurenshu']){
                        $shoplist[$key]['cart_gorenci'] = $shoplist[$key]['shenyurenshu'];
                    }
                    $MoenyCount+=$shoplist[$key]['yunjiage']*$shoplist[$key]['cart_gorenci'];
                    $shoplist[$key]['cart_xiaoji']=substr(sprintf("%.3f",$shoplist[$key]['yunjiage'] * $shoplist[$key]['cart_gorenci']),0,-1);
                    $shoplist[$key]['cart_shenyu']=$shoplist[$key]['zongrenshu']-$shoplist[$key]['canyurenshu'];
                    $scookies_arr[$key]['shenyu'] = $shoplist[$key]['cart_shenyu'];
                    $scookies_arr[$key]['num'] = $shoplist[$key]['cart_gorenci'];
                    $scookies_arr[$key]['money'] = intval($shoplist[$key]['yunjiage']);
                    $scookies_arr['MoenyCount'] += intval($shoplist[$key]['cart_xiaoji']);
                }else{
                    unset($shoplist[$key]);
                }
            }
            if(count($shoplist) < 1){
                $scookies_arr = '0';
                $this->db->Autocommit_rollback();
                if($shopguoqi){
                    return '限时揭晓过期商品不能购买!';
                }else{
                    return '购物车里没有商品!';
                }
            }
        }else{
            $scookies_arr = '0';
            $this->db->Autocommit_rollback();
            return '购物车里商品已经卖完或已下架!';
        }


        $this->MoenyCount=substr(sprintf("%.3f",$MoenyCount),0,-1);

        /**
         *	最多能抵扣多少钱
         **/
        if($this->fufen){
            if($this->fufen >= $this->members['score']){
                $this->fufen = $this->members['score'];
            }
            $fufen = System::load_app_config("user_fufen",'','member');
            if($fufen['fufen_yuan']){
                $this->fufen_to_money  = intval($this->fufen / $fufen['fufen_yuan']);
                if($this->fufen_to_money >= $this->MoenyCount){
                    $this->fufen_to_money = $this->MoenyCount;
                    $this->fufen = $this->fufen_to_money * $fufen['fufen_yuan'];
                }
            }else{
                $this->fufen_to_money = 0;
                $this->fufen = 0;
            }
        }else{
            $this->fufen_to_money = 0;
            $this->fufen = 0;
        }

        //总支付价格
        $this->MoenyCount = $this->MoenyCount - $this->fufen_to_money;
        $this->shoplist=$shoplist;
        $this->scookies_arr = $scookies_arr;
        $arr = array();
        array_push($arr,$this->MoenyCount);
        array_push($arr,$shoplist);
        array_push($arr,$scookies_arr);
        return ($arr);
        //return 'ok';
	}

	
	/* 充值 data 其他数据 */
	private function addmoney_record($money=null,$data=null){

		$uid=$this->members['uid'];
		$dingdancode = pay_get_dingdan_code('C');		//订单号
		if(!is_array($this->pay_type)){
			return 'not_pay';
		}
		$pay_type = $this->pay_type['pay_name'];
		$time = time();
		if(!empty($data)){
			$scookies = $data;
		}else{
			$scookies = '0';
		}
		$score = $this->fufen;
		$query = $this->db->Query("INSERT INTO `@#_member_addmoney_record` (`uid`, `code`, `money`, `pay_type`, `status`,`time`,`score`,`scookies`) VALUES ('$uid', '$dingdancode', '$money', '$pay_type','未付款', '$time','$score','$scookies')");
		if($query){
			$this->db->Autocommit_commit();
		}else{
			$this->db->Autocommit_rollback();
			return false;
		}

		$pay_type = $this->pay_type;
		$paydb = System::load_app_class($pay_type['pay_class'],'pay');
		$pay_type['pay_key'] = unserialize($pay_type['pay_key']);

		$config=array();
		$config['id'] = $pay_type['pay_key']['id']['val'];			//支付合作ID
		$config['key'] = $pay_type['pay_key']['key']['val'];		//支付KEY

		$config['shouname'] = _cfg('web_name');						//收款方
		$config['title'] = _cfg('web_name');						//付款项目
		$config['money'] = $money;									//付款金额$money
		$config['type']  = $pay_type['pay_type'];					//支付方式：	即时到帐1   中介担保2


		$config['ReturnUrl']  = G_WEB_PATH.'/index.php/pay/'.$pay_type['pay_class'].'_url/qiantai/';	//前台回调
		$config['NotifyUrl']  = G_WEB_PATH.'/index.php/pay/'.$pay_type['pay_class'].'_url/houtai/';		//后台回调


		$config['pay_bank'] = $this->pay_type['pay_bank'];

		$config['code'] = $dingdancode;
		$config['pay_type_data'] = $pay_type['pay_key'];

		$paydb->config($config);
		$paydb->send_pay();


		return true;
	}

	//生成订单
	private function set_dingdan($userId,$userName,$pay_type='',$dingdanzhui='',$shoplist,$goodsinfo){
            $this->db=System::load_sys_class('model');
			$uid=$userId;//$this->members['uid'];
			$uphoto = '';//$this->members['img'];
			$username = $userName;//get_user_name($this->members);

		$this->writeLogFile("old_userId");
		$this->writeLogFile($uid);
		$this->writeLogFile($username);
		$this->writeLogFile("old_userId_end");


		$real_sql = "select uid from `@#_member` where `ucenterid` = '$userName' limit 1";
		$this->writeLogFile("process_real_sql:");
		$newTmpId = $this->db->GetOne($real_sql);
		if(!empty($newTmpId['uid']))
		{
			$uid = $newTmpId['uid'];
		}
		$this->writeLogFile("new_uid");
		$this->writeLogFile($uid);
		$this->writeLogFile("new_uid_end");

			$insert_html='';
			$this->dingdancode = $dingdancode= pay_get_dingdan_code($dingdanzhui);		//订单号
			$this->writeLogFile($dingdancode);
            $dingdancode_tmp = 1;	//多个商品相同订单
//			if(count($shoplist)>1){
//					$dingdancode_tmp = 1;	//多个商品相同订单
//			}else{
//					$dingdancode_tmp = 0;	//单独商品订单
//			}

			$ip = _get_ip_dizhi();

			//订单时间
			$time=sprintf("%.3f",microtime(true));
			$this->MoenyCount=0;
			foreach($shoplist as $key=>$shop){
//					$ret_data = array();
//					pay_get_shop_codes($shop['cart_gorenci'],$shop,$ret_data);
//					$this->dingdan_query = $ret_data['query'];
//					if(!$ret_data['query'])$this->dingdan_query = false;
//					$codes = $ret_data['user_code'];									//得到的购买码
//					$codes_len= intval($ret_data['user_code_len']);						//得到购买码个数
//					$money=$codes_len * $shop['yunjiage'];								//单条商品的总价格
//					$this->MoenyCount += $money;										//总价格
					$status='未付款,未发货,未完成';
//					$shop['canyurenshu'] = intval($shop['canyurenshu']) + $codes_len;
//					$shop['goods_count_num'] = $codes_len;
//                    $shoplist[$key] = $shop;
//                    $this->shoplist[$key] = $shop;
//					if($codes_len){
//						$insert_html.="('$dingdancode','$dingdancode_tmp','$uid','$username','$uphoto','$shop[id]','$shop[title]','$shop[qishu]','$codes_len','$money','$codes','$pay_type','$ip','$status','$time'),";
//					}
                $codes_len = 0;
                $money = 0;
                $codes = '';

                $shoplist[$key] = $shop;
                $this->shoplist[$key] = $shop;
                $insert_html.="('$dingdancode','$dingdancode_tmp','$uid','$username','$uphoto','$shop[id]','$shop[title]','$shop[qishu]','$codes_len','$money','$codes','$pay_type','$ip','$status','$time','$time','$time'),";

            }
            $this->writeLogFile("xxxxxxxxinsert_htmlxxxxxxxxxxxxxxxxxxx");
            $this->writeLogFile($insert_html);
            $this->writeLogFile("xxxxxxxx222insert_htmlxxxxxxxxxxxxxxxxxxx");
            $sql="INSERT INTO `@#_member_go_record` (`code`,`code_tmp`,`uid`,`username`,`uphoto`,`shopid`,`shopname`,`shopqishu`,`gonumber`,`moneycount`,`goucode`,`pay_type`,`ip`,`status`,`time`,`paytime`,`paytime2`) VALUES ";
			$sql.=trim($insert_html,',');
			if(empty($insert_html)){
				return [false,$dingdancode];
			}
//            return [$this->db,$this->db];
//            exit(1);
            $insert_html1.="('$dingdancode','$uid','$goodsinfo','$time'),";
            $sql1 ="INSERT INTO `@#_member_dingdan` (`code`,`uid`,`info`,`time`) VALUES ";
			$sql1.=trim($insert_html1,',');
			if(empty($insert_html1)){
				return [false,$dingdancode];
			}
			$this->writeLogFile("======");
			$this->writeLogFile($insert_html1);
			$this->writeLogFile("======");
			$ret1 = $this->db->Query($sql1);
			$this->writeLogFile("==ret1====");
			$this->writeLogFile($ret1);
			if($ret1){
                
            }else{
                $this->db->Autocommit_rollback();
                return [$ret1,$dingdancode];
            }
			
	    $this->writeLogFile("==sql:===".$sql);	
            $ret = $this->db->Query($sql);
            if($ret){
				$this->writeLogFile("==ret====");
				$this->writeLogFile($ret);
                $this->db->Autocommit_commit();
		$this->writeLogFile("==xxret===".$ret);	
		$this->writeLogFile("==dingdancode===".$dingdancode);	
                return [$ret,$dingdancode];
            }else{
                $this->db->Autocommit_rollback();
                return [$ret,$dingdancode];
            }

        //$this->db->Query("set global max_allowed_packet = 2*1024*1024*10");
			//return [$ret,$dingdancode];
	}

	
	
	/**
	*	开始支付
	**/
	public function go_pay($pay_checkbox){

		if($this->members['money'] >= $this->MoenyCount){
			$uid=$this->members['uid'];
			$pay_1 =  $this->pay_bag();
			if(!$pay_1){return $pay_1;}
			$dingdancode=$this->dingdancode;
			$pay_2 = pay_go_fund($this->goods_count_num);
			$pay_3 = pay_go_yongjin($uid,$dingdancode);
			return $pay_1;
		}
		if(!is_array($this->pay_type)){
			return 'not_pay';
		}
		if(is_array($this->scookies_arr)){
			$scookie = serialize($this->scookies_arr);
		}else{
			$scookie= '0';
		}
		if($pay_checkbox){
			$money = $this->MoenyCount - $this->members['money'];
			return $this->addmoney_record($money,$scookie);
		}else{
			//全额支付
			$this->MoenyCount;
			return $this->addmoney_record($this->MoenyCount,$scookie);
		}
		exit;
	}


	//账户里支付
	public function pay_bag($userId,$userName,$shoplist,$goodsinfo){
		$time=time();
		$uid=$userId;//$this->members['uid'];
		$fufen = System::load_app_config("user_fufen",'','member');
		list($result,$dingdancode) = $this->set_dingdan($uid,$userName,'账户','D',$shoplist,$goodsinfo);
        $query_1 = $result;
        $this->query_1 = $query_1;
        $this->dingdancode = $dingdancode;
        return [$result,$dingdancode];
        /*
		//会员购买过账户剩余金额
		$Money = $this->members['money'] - $this->MoenyCount + $this->fufen_to_money;
		$query_fufen = true;
		$pay_zhifu_name = '账户';
		if($this->fufen_to_money){
			$myfufen = $this->members['score'] - $this->fufen;
			$query_fufen = $this->db->Query("UPDATE `@#_member` SET `score`='$myfufen' WHERE (`uid`='$uid')");
			$pay_zhifu_name = '福分';
			$this->MoenyCount = $this->fufen;
		}

		//添加用户经验
		$jingyan = $this->members['jingyan'] + $fufen['z_shoppay'];
		$query_jingyan = $this->db->Query("UPDATE `@#_member` SET `jingyan`='$jingyan' WHERE (`uid`='$uid')");	//经验值

		//更新用户账户金额
		$query_2 = $this->db->Query("UPDATE `@#_member` SET `money`='$Money' WHERE (`uid`='$uid')");			//金额
		$query_3 = $info = $this->db->GetOne("SELECT * FROM  `@#_member` WHERE (`uid`='$uid') LIMIT 1");
		$query_4 = $this->db->Query("INSERT INTO `@#_member_account` (`uid`, `type`, `pay`, `content`, `money`, `time`) VALUES ('$uid', '-1', '$pay_zhifu_name', '购买了商品', '{$this->MoenyCount}', '$time')");
		$query_5 = true;
		$query_insert = true;


		$goods_count_num = 0;
		foreach($this->shoplist as $shop):
			if($shop['canyurenshu'] >= $shop['zongrenshu'] && $shop['maxqishu'] >= $shop['qishu']){
					$this->db->Query("UPDATE `@#_shoplist` SET `canyurenshu`=`zongrenshu`,`shenyurenshu` = '0' where `id` = '$shop[id]'");
			}else{
				$sellnum = $this->db->GetOne("select sum(gonumber) as sellnum from `@#_member_go_record` where `shopid` = '$shop[id]'");
				$sellnum = $sellnum['sellnum'];
				$shenyurenshu = $shop['zongrenshu'] - $sellnum;
				$query = $this->db->Query("UPDATE `@#_shoplist` SET `canyurenshu` = '$sellnum',`shenyurenshu` = '$shenyurenshu' WHERE `id`='$shop[id]'");

				// $shenyurenshu = $shop['zongrenshu'] - $shop['canyurenshu'];
				// $query = $this->db->Query("UPDATE `@#_shoplist` SET `canyurenshu` = '$shop[canyurenshu]',`shenyurenshu` = '$shenyurenshu' WHERE `id`='$shop[id]'");
				if(!$query)$query_5=false;
			}
			$goods_count_num += $shop['goods_count_num'];
		endforeach;

		//添加福分
		if(!$this->fufen_to_money){
			$mygoscore = $fufen['f_shoppay']*$goods_count_num;
			$mygoscore_text =  "购买了{$goods_count_num}人次商品";
			$myscore = $this->members['score'] + $mygoscore;
			$query_add_fufen_1 = $this->db->Query("UPDATE `@#_member` SET `score`= '$myscore' WHERE (`uid`='$uid')");
			$query_add_fufen_2 = $this->db->Query("INSERT INTO `@#_member_account` (`uid`, `type`, `pay`, `content`, `money`, `time`) VALUES ('$uid', '1', '福分', '$mygoscore_text', '$mygoscore', '$time')");
			$query_fufen = ($query_add_fufen_1 && $query_add_fufen_2);
		}*/
        /*$query_fufen = true;
        $query_jingyan = true;
        $query_2 = true;
        $query_3 = $info = $this->db->GetOne("SELECT * FROM  `@#_member` WHERE (`uid`='$uid') LIMIT 1");

        $pay_zhifu_name = '账户';
        $query_4 = $this->db->Query("INSERT INTO `@#_member_account` (`uid`, `type`, `pay`, `content`, `money`, `time`) VALUES ('$uid', '-1', '$pay_zhifu_name', '购买了商品', '$Money', '$time')");

        $query_5 =true;
        $query_insert = true;

		$dingdancode=$this->dingdancode;
		$query_6 = $this->db->Query("UPDATE `@#_member_go_record` SET `status`='未付款,未发货,未完成' WHERE `code`='$dingdancode' and `uid` = '$uid'");
		$query_7 = $this->dingdan_query;
		$query_8 = $this->db->Query("UPDATE `@#_caches` SET `value`=`value` + $goods_count_num WHERE `key`='goods_count_num'");
		$this->goods_count_num = $goods_count_num;

		if($query_fufen && $query_jingyan && $query_1 && $query_2 && $query_3 && $query_4 && $query_5 && $query_6 && $query_7 && $query_insert && $query_8){
			//if($info['money'] == $Money){
				$this->db->Autocommit_commit();
					foreach($this->shoplist as $shop):
						if($shop['canyurenshu'] >= $shop['zongrenshu'] && $shop['maxqishu'] >= $shop['qishu']){
								$this->db->Autocommit_start();
								$query_insert = pay_insert_shop($shop,'add');
								if(!$query_insert){
									$this->db->Autocommit_rollback();
								}else{
									$this->db->Autocommit_commit();
								}
								$this->db->Query("UPDATE `@#_shoplist` SET `canyurenshu`=`zongrenshu`,`shenyurenshu` = '0' where `id` = '$shop[id]'");
						}
					endforeach;
				return [true,$dingdancode];
//			}else{
//				$this->db->Autocommit_rollback();
//				return false;
//			}
		}else{
			$this->db->Autocommit_rollback();
			return [false,$dingdancode];
		}*/

	}

    public function payOk($userId,$Money,$goodsInfos,$dingdancode) {
        $this->db=System::load_sys_class('model');
        $time=time();
        $uid= intval($userId);//$this->members['uid'];
        $query_fufen = true;
        $query_jingyan = true;
        $query_2 = true;
        $query_3 = $info = $this->db->GetOne("SELECT * FROM  `@#_member` WHERE (`uid`=$uid) LIMIT 1");
//        $this->writeLogFile("===info===");
//        $this->writeLogFile($info);
//        $this->writeLogFile("===info end===");
        $goods_id_num = '';
        if (is_array($goodsInfos)) {
            foreach ($goodsInfos as $key => $val) {
                //$goodsInfos[$key] = $val;
                $goods_id_num .= $key;
                $goods_id_num .= ',';
                $goods_id_num .= $val;
            }
        }
		$this->writeLogFile("===goods_id_num===");
		$this->writeLogFile($goods_id_num);
//        echo $goods_id_num;
//        exit(1);
        $pay_zhifu_name = '账户';
        $mygo_text =  "购买了{$goods_id_num}人次商品";
		$sql = "INSERT INTO `@#_member_account` (`uid`, `type`, `pay`, `content`, `money`, `time`) VALUES ('$uid', '-1', '$pay_zhifu_name', '$mygo_text', '$Money', '$time')";
        $this->writeLogFile("===query_4===");
		$this->writeLogFile($sql);
		$query_4 = $this->db->Query($sql);
//        $this->writeLogFile("===query_4===");
//        $this->writeLogFile($query_4);
//        $this->writeLogFile("===query_4 end===");
        $query_5 =true;
        $query_insert = true;

        $dingdancode=$dingdancode;

        $query_6 = false;
        $query_7 = $this->dingdan_query;

        $Cartlist=null;
        $values = $this->db->GetList("SELECT cart FROM `@#_cart` where `uid` = $userId");

        foreach ($values as $key => $val) {
            $data = json_decode($val['cart'], true);
            // $this->Cartlist = $data;
            $Cartlist = $data;
        }
        //$Cartlist = $this->Cartlist;

        $shopids='';			//商品ID
        if(is_array($Cartlist)){
            foreach($Cartlist as $key => $val){
                $shopids.=intval($key).',';
            }
            $shopids=str_replace(',0','',$shopids);
            $shopids=trim($shopids,',');

        }
        //$this->writeLogFile("====shopids===");
        //$this->writeLogFile($shopids);

        $shoplist=array();		//商品信息
        if($shopids!=NULL){
            $shoplist=$this->db->GetList("SELECT * FROM `@#_shoplist` where `id` in($shopids) and `q_uid` is null for update",array("key"=>"id"));
        }else{
            //$this->db->Autocommit_rollback();
            return [false,'0购物车内没有商品!'];
        }

        $MoenyCount= 0;
        $shopguoqi = 0;
        if(count($shoplist)>=1){
            $scookies_arr = array();
            $scookies_arr['MoenyCount'] = 0;
            foreach($Cartlist as $key => $val){
                $key=intval($key);
                if(isset($shoplist[$key]) && $shoplist[$key]['shenyurenshu'] != 0){
                    if(($shoplist[$key]['xsjx_time'] != '0') && $shoplist[$key]['xsjx_time'] < time()){
                        unset($shoplist[$key]);
                        $shopguoqi = 1;
                        continue;
                    }
                    $shoplist[$key]['cart_gorenci']=$val['num'] ? $val['num'] : 1;
                    if($shoplist[$key]['cart_gorenci'] >= $shoplist[$key]['shenyurenshu']){
                        $shoplist[$key]['cart_gorenci'] = $shoplist[$key]['shenyurenshu'];
                    }
                    $MoenyCount+=$shoplist[$key]['yunjiage']*$shoplist[$key]['cart_gorenci'];
                    $shoplist[$key]['cart_xiaoji']=substr(sprintf("%.3f",$shoplist[$key]['yunjiage'] * $shoplist[$key]['cart_gorenci']),0,-1);
                    $shoplist[$key]['cart_shenyu']=$shoplist[$key]['zongrenshu']-$shoplist[$key]['canyurenshu'];
                    $scookies_arr[$key]['shenyu'] = $shoplist[$key]['cart_shenyu'];
                    $scookies_arr[$key]['num'] = $shoplist[$key]['cart_gorenci'];
                    $scookies_arr[$key]['money'] = intval($shoplist[$key]['yunjiage']);
                    $scookies_arr['MoenyCount'] += intval($shoplist[$key]['cart_xiaoji']);
                }else{
                    unset($shoplist[$key]);
                }
            }
            if(count($shoplist) < 1){
                $scookies_arr = '0';
                if($shopguoqi){
                    return  [false,'1限时揭晓过期商品不能购买!'];
                }else{
                    return  [false,'2购物车里没有商品!'];
                }
            }
        }else{
            $scookies_arr = '0';
            return [false,'3购物车里商品已经卖完或已下架!'];
        }


        $dingdancode_tmp = 1;
        $status='已付款,未发货,未完成';
        
        //$this->writeLogFile("====shoplist===");
        //$this->writeLogFile($shoplist);
        //$this->writeLogFile("====shoplist===");
        //$this->db->Autocommit_start();
        foreach($shoplist as $key=>$shop){
            if(!is_array($shop)) {
                continue;
            }
            $ret_data = array();
			$codes = '';
			$money = 0;
			$codes_len = 0;
			
			//$this->writeLogFile("====shop===");
			//$this->writeLogFile($shop);
			//$this->writeLogFile("====shop===");
            pay_get_shop_codes($shop['cart_gorenci'],$shop,$ret_data);
            //$this->writeLogFile("====ret_data===");
            //$this->writeLogFile($ret_data);
            //$this->writeLogFile($ret_data['user_code']);
			//$this->writeLogFile($ret_data['user_code_len']);
            //$this->writeLogFile("====ret_data===");
            $codes .= $ret_data['user_code'];									//得到的购买码
            $codes_len = $codes_len + intval($ret_data['user_code_len']);						//得到购买码个数
            $money = $money + intval($ret_data['user_code_len']) * $shop['yunjiage'];								//单条商品的总价格
            $this->MoenyCount += $money;										//总价格

            $shop['canyurenshu'] = intval($shop['canyurenshu']) + $codes_len;
            $shop['goods_count_num'] = $codes_len;
            $shoplist[$key] = $shop;
            $this->shoplist[$key] = $shop;
            //$codes .= ',';
			//$codes = rtrim($codes, ",");
			
			$sql = "UPDATE `@#_member_go_record` SET `status`='$status', `code_tmp`=$dingdancode_tmp,`gonumber` = $codes_len ,`moneycount`=$money, `goucode`='$codes',`paytime`='$time' WHERE `code`='$dingdancode' and `uid` = '$uid' and `shopid` =$shop[id] and `shopqishu` = $shop[qishu]";
			//$this->writeLogFile("====query_6 sql===");
			//$this->writeLogFile($sql);
			//$this->writeLogFile("====query_6 sql===");
			$query_6 = $this->db->Query($sql);
        }

//        $this->writeLogFile("====shoplist 22===");
//        $this->writeLogFile($shoplist);
//        $this->writeLogFile("====shoplist 22===");
        $goods_count_num = 0;
        $gocodes = array();
        foreach($shoplist as $key=>$shop):
            if(!is_array($shop)) {
                continue;
            }
//            $this->writeLogFile("====shop===");
//            $this->writeLogFile($shop);
//            $this->writeLogFile("====shop xx===");
            $sql = "select * from `@#_member_go_record` where `code` = '$dingdancode'  and `uid` = '$userId' and `shopid` = '$shop[id]' and `shopqishu` = '$shop[qishu]' limit 1";
            $go_record_info  = $this->db->GetOne($sql);
            $table = '@#_'.$shop['codes_table'];
            $codes_len = $this->db->GetOne("select s_len from `$table` where `s_id` = '$shop[id]' ");
            $buy_num = $go_record_info['gonumber'];
            if($codes_len < $buy_num) {
                $buy_num = $codes_len;
            }
            $shop['canyurenshu'] = $buy_num;
            //array_push($gocodes,$go_record_info['goucode']);
            $goods_count_num += $shop['canyurenshu'];
            $shop['goods_count_num'] = $goods_count_num;

            if($shop['canyurenshu'] >= $shop['zongrenshu'] && $shop['maxqishu'] >= $shop['qishu']){

                $results = $this->db->Query("select * from `@#_shoplist` WHERE `id`='$shop[id]'");
                if($results['canyurenshu'] + $results['shenyurenshu'] != $results['zongrenshu'])
                {
                    continue;
                }
                $this->db->Query("UPDATE `@#_shoplist` SET `canyurenshu`=`zongrenshu`,`shenyurenshu` = '0' where `id` = '$shop[id]'");

                $shop['canyurenshu'] = $results['canyurenshu'] + $shop['canyurenshu'];
                $shop['shenyurenshu'] = $results['shenyurenshu'] - $shop['canyurenshu'];

            }else{
//                $shenyurenshu = $shop['zongrenshu'] - $shop['canyurenshu'];
                $results = $this->db->Query("select * from `@#_shoplist` WHERE `id`='$shop[id]'");
                if($results['canyurenshu'] + $results['shenyurenshu'] != $results['zongrenshu'])
                {
                    continue;
                }
                $query = $this->db->Query("UPDATE `@#_shoplist` SET `canyurenshu` = `canyurenshu` + $buy_num,`shenyurenshu` = `shenyurenshu` - $buy_num WHERE `id`='$shop[id]'");
                if(!$query)$query_5=false;

                $shop['canyurenshu'] = $results['canyurenshu'] + $shop['canyurenshu'];
                $shop['shenyurenshu'] = $results['shenyurenshu'] - $shop['canyurenshu'];
            }
            $shop['gocode'] = $go_record_info['goucode'];

        endforeach;

//        $this->writeLogFile("===shoplist===");
//        $this->writeLogFile($shoplist);

        $query_8 = $this->db->Query("UPDATE `@#_caches` SET `value`=`value` + $goods_count_num WHERE `key`='goods_count_num'");
//        $this->goods_count_num = $goods_count_num;
        $this->query_1 = true;
//        echo json_encode($query_3);
//        exit(1);
        //echo $query_fufen.'|'.$query_jingyan.'|'.$this->query_1.'|'.$query_2.'|'.$query_3.'|'.$query_4.'|'.$query_5.'|'.$query_6.'|'.$query_7.'|'.$query_insert.'|'.$query_8.'|';
        //exit(1);
//        $this->writeLogFile("===query_fufen===");
//        $this->writeLogFile($query_fufen);
//        $this->writeLogFile("===query_jingyan===");
//        $this->writeLogFile($query_jingyan);
//        $this->writeLogFile("===this->query_1 ===");
//        $this->writeLogFile($this->query_1);
//        $this->writeLogFile("===query_2===");
//        $this->writeLogFile($query_2);
//        $this->writeLogFile("===query_3===");
//        //$this->writeLogFile($query_3);
//        $this->writeLogFile("===query_4===");
//        $this->writeLogFile($query_4);
//        $this->writeLogFile("===query_5===");
//        $this->writeLogFile($query_5);
//        $this->writeLogFile("===query_6===");
//        $this->writeLogFile($query_6);
//        $this->writeLogFile("===query_7===");
//        $this->writeLogFile($query_7);
//        $this->writeLogFile("===query_insert===");
//        $this->writeLogFile($query_insert);
//        $this->writeLogFile("===query_8===");
//        $this->writeLogFile($query_8);
         //$this->db->Autocommit_commit();

        if($query_fufen && $query_jingyan && $this->query_1 && $query_2 && $query_3 && $query_4 && $query_5 && $query_6 && $query_7 && $query_insert && $query_8){
            //if($info['money'] == $Money){
            //$this->writeLogFile($this->db);
            //$this->db->Autocommit_commit();
            $this->writeLogFile("===shoplist===");
            //$this->writeLogFile(gettype($shoplist));
            $this->writeLogFile($shoplist);
            $this->writeLogFile("===shoplist===");

            foreach($shoplist as $key=>$shop):
//                $this->writeLogFile("===shop===");
//                $this->writeLogFile(gettype($shop));
//                $this->writeLogFile($shop);
//                $this->writeLogFile("===shop===");
                if($shop['canyurenshu'] >= $shop['zongrenshu'] && $shop['maxqishu'] >= $shop['qishu']){
                    $this->writeLogFile("===Autocommit_start===");
                    $this->db->Autocommit_start();
                    $query_insert = pay_insert_shop($shop,'add');
                    if(!$query_insert){
                        $this->db->Autocommit_rollback();
                    }else{
                        $this->db->Autocommit_commit();
                    }
                    $this->db->Query("UPDATE `@#_shoplist` SET `canyurenshu`=`zongrenshu`,`shenyurenshu` = '0' where `id` = '$shop[id]'");
                }
            endforeach;
//			}else{
//				$this->db->Autocommit_rollback();
//				return false;
//			}
        }else{
            $this->db->Autocommit_rollback();
            return [false,$dingdancode];
        }
        $query_9 = $this->db->Query("UPDATE `@#_member_go_record` SET `status`='已付款,已发货,未完成',`paytime2`='$time' WHERE `code`='$dingdancode' and `uid` = '$uid'");
        $query_10 = $this->db->Query("update `@#_cart` SET `cart` = '{}' WHERE  `uid` = '$uid'");

        return [true,$dingdancode];
    }

	public function pay_user_go_shop($uid=null,$gid=null,&$num=null){
		if(empty($uid) || empty($gid) || empty($num)){
			return false;
		}
		$uid = intval($uid);$gid = intval($gid);$num = intval($num);
		$this->db=System::load_sys_class('model');
		$this->db->Autocommit_start();
		$member = $this->db->GetOne("select * from `@#_member` where `uid` = '$uid' for update");
		$goodinfo = $this->db->GetOne("select * from `@#_shoplist` where `id` = '$gid' and `shenyurenshu` != '0' for update");
		if(!$goodinfo['shenyurenshu']){
			$this->db->Autocommit_rollback();
			return false;
		}
		if($goodinfo['shenyurenshu'] < $num){
			$num = $goodinfo['shenyurenshu'];
		}
		$if_money = $goodinfo['yunjiage'] * $num;
		$this->members = $member;
		$this->MoenyCount = $if_money;
		$goodinfo['goods_count_num'] = $num;
		$goodinfo['cart_gorenci'] = $num;

		$this->shoplist = array();
		$this->shoplist[0] = $goodinfo;

		if($member && $goodinfo && $member['money'] >= $if_money){

			$uid=$member['uid'];
			$pay_1 =  $this->pay_bag();
			if(!$pay_1){return $pay_1;}
			$dingdancode=$this->dingdancode;
			$pay_2 = pay_go_fund($this->goods_count_num);
			$pay_3 = pay_go_yongjin($uid,$dingdancode);
			return $pay_1;

		}else{
			$this->db->Autocommit_rollback();
			return false;
		}
	}

    public  function writeLogFile($array1) {
        //$path=date('Y-m-d-H-i-s').".log";
        $path=date('Y-m-d').".log";

        if(is_array($array1))
        {
            file_put_contents($path, count($array1)."\r\n", FILE_APPEND);
            foreach ($array1 as  $key=>$value)
            {
                file_put_contents($path, $key."==>\r\n", FILE_APPEND);
                //echo $value."<br/>";
                if(is_array($value))
                {
                    foreach ($value as $key1=>$value1)
                    {
                        if(is_array($value1))
                        {
                            foreach ($value1 as $key2=>$value2)
                            {
                                file_put_contents($path, $key2.":==>".$value2."$", FILE_APPEND);
                            }
                        } else {
                            file_put_contents($path, $key1.":".$value1."$", FILE_APPEND);
                        }

                    }
                } else {
                    file_put_contents($path, $key."xx:".$value."$", FILE_APPEND);
                }

                file_put_contents($path, "\r\n", FILE_APPEND);
            }
        }
        else
        {
            file_put_contents($path, $array1."\r\n", FILE_APPEND);
        }
    }
}
?>
